/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.neilcsmith.praxis.live.audio.options;

import net.neilcsmith.praxis.audio.AudioSettings;

final class GeneralAudioPanel extends javax.swing.JPanel {

    private final static String[] libraries = new String[]{
        "JavaSound", "JACK"
    };
    private final static String[] libraryDisplay = new String[]{
        "JavaSound", "Jack Audio Connection Kit"
    };
    private final static int[] samplerates = new int[]{
        22050, 32000, 44100, 48000, 88200, 96000
    };
    private final static String[] samplerateDisplay = new String[]{
        "22050", "32000", "44100", "48000", "88200", "96000"
    };
    private final static int[] buffersizes = new int[]{
        32, 64, 128, 256, 512, 1024
    };
    private final static String[] buffersizeDisplay = new String[]{
        "32", "64", "128", "256", "512", "1024"
    };
    private final GeneralAudioOptionsPanelController controller;

    GeneralAudioPanel(GeneralAudioOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        initChoosers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        defaultConfigPanel = new javax.swing.JPanel();
        defaultConfigDescription = new javax.swing.JLabel();
        libraryLbl = new javax.swing.JLabel();
        libraryChooser = new javax.swing.JComboBox();
        samplerateLbl = new javax.swing.JLabel();
        samplerateChooser = new javax.swing.JComboBox();
        buffersizeLbl = new javax.swing.JLabel();
        buffersizeChooser = new javax.swing.JComboBox();

        defaultConfigPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(GeneralAudioPanel.class, "GeneralAudioPanel.defaultConfigPanel.border.title"))); // NOI18N

        defaultConfigDescription.setForeground(javax.swing.UIManager.getDefaults().getColor("textInactiveText"));
        defaultConfigDescription.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(defaultConfigDescription, org.openide.util.NbBundle.getMessage(GeneralAudioPanel.class, "GeneralAudioPanel.defaultConfigDescription.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(libraryLbl, org.openide.util.NbBundle.getMessage(GeneralAudioPanel.class, "GeneralAudioPanel.libraryLbl.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(samplerateLbl, org.openide.util.NbBundle.getMessage(GeneralAudioPanel.class, "GeneralAudioPanel.samplerateLbl.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(buffersizeLbl, org.openide.util.NbBundle.getMessage(GeneralAudioPanel.class, "GeneralAudioPanel.buffersizeLbl.text")); // NOI18N

        javax.swing.GroupLayout defaultConfigPanelLayout = new javax.swing.GroupLayout(defaultConfigPanel);
        defaultConfigPanel.setLayout(defaultConfigPanelLayout);
        defaultConfigPanelLayout.setHorizontalGroup(
            defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(defaultConfigPanelLayout.createSequentialGroup()
                .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultConfigPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(buffersizeLbl)
                            .addComponent(libraryLbl)
                            .addComponent(samplerateLbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buffersizeChooser, 0, 485, Short.MAX_VALUE)
                            .addComponent(samplerateChooser, 0, 485, Short.MAX_VALUE)
                            .addComponent(libraryChooser, 0, 485, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultConfigPanelLayout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(defaultConfigDescription, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        defaultConfigPanelLayout.setVerticalGroup(
            defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(defaultConfigPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(libraryChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(libraryLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(samplerateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(samplerateLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(defaultConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buffersizeLbl)
                    .addComponent(buffersizeChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 233, Short.MAX_VALUE)
                .addComponent(defaultConfigDescription)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(defaultConfigPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(defaultConfigPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initChoosers() {
        for (String lib : libraryDisplay) {
            libraryChooser.addItem(lib);
        }
        for (String srate : samplerateDisplay) {
            samplerateChooser.addItem(srate);
        }
        for (String bsize : buffersizeDisplay) {
            buffersizeChooser.addItem(bsize);
        }
    }

    void load() {
        loadLibrary();
        loadSamplerate();
        loadBuffersize();
    }

    void store() {
        saveLibrary();
        saveSamplerate();
        saveBuffersize();
    }

    private void loadLibrary() {
        String lib = AudioSettings.getLibrary();
        int idx = -1;
        for (int i = 0; i < libraries.length; i++) {
            if (libraries[i].equals(lib)) {
                idx = i;
                break;
            }
        }
        if (idx == -1) {
            libraryChooser.addItem(lib);
            libraryChooser.setSelectedItem(lib);
        } else {
            libraryChooser.setSelectedIndex(idx);
        }
    }

    private void saveLibrary() {
        int idx = libraryChooser.getSelectedIndex();
        if (idx >= libraries.length) {
            // custom
            return;
        }
        String current = AudioSettings.getLibrary();
        String selected = libraries[idx];
        if (!selected.equals(current)) {
            AudioSettings.setLibrary(selected);
        }
    }
    
    private void loadSamplerate() {
        int srate = AudioSettings.getSamplerate();
        int idx = -1;
        for (int i = 0; i < samplerates.length; i++) {
            if (srate == samplerates[i]) {
                idx = i;
                break;
            }
        }
        if (idx == -1) {
            String custom = Integer.toString(srate);
            samplerateChooser.addItem(custom);
            samplerateChooser.setSelectedItem(custom);
        } else {
            samplerateChooser.setSelectedIndex(idx);
        }
    }
    
    private void saveSamplerate() {
        int idx = samplerateChooser.getSelectedIndex();
        if (idx >= samplerates.length) {
            // custom
            return;
        }
        int current = AudioSettings.getSamplerate();
        int selected = samplerates[idx];
        if (current != selected) {
            AudioSettings.setSamplerate(selected);
        }
    }
    
    private void loadBuffersize() {
        int bsize = AudioSettings.getBuffersize();
        int idx = -1;
        for (int i = 0; i < buffersizes.length; i++) {
            if (bsize == buffersizes[i]) {
                idx = i;
                break;
            }
        }
        if (idx == -1) {
            String custom = Integer.toString(bsize);
            buffersizeChooser.addItem(custom);
            buffersizeChooser.setSelectedItem(custom);
        } else {
            buffersizeChooser.setSelectedIndex(idx);
        }
    }
    
    private void saveBuffersize() {
        int idx = buffersizeChooser.getSelectedIndex();
        if (idx >= buffersizes.length) {
            // custom
            return;
        }
        int current = AudioSettings.getBuffersize();
        int selected = buffersizes[idx];
        if (current != selected) {
            AudioSettings.setBuffersize(selected);
        }
    }

    boolean valid() {
        // check whether form is consistent and complete
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox buffersizeChooser;
    private javax.swing.JLabel buffersizeLbl;
    private javax.swing.JLabel defaultConfigDescription;
    private javax.swing.JPanel defaultConfigPanel;
    private javax.swing.JComboBox libraryChooser;
    private javax.swing.JLabel libraryLbl;
    private javax.swing.JComboBox samplerateChooser;
    private javax.swing.JLabel samplerateLbl;
    // End of variables declaration//GEN-END:variables
}
